package com.nsgf.library.ui.page;

import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.kunminx.architecture.ui.page.DataBindingConfig;
import com.kunminx.architecture.ui.state.State;
import com.nsgf.library.BR;
import com.nsgf.library.R;
import com.nsgf.library.databinding.ActivityPatrolObjectSelectionBinding;

import java.util.ArrayList;
import java.util.List;

/**
 * 巡查对象选择页面
 */
public class PatrolObjectSelectionActivity extends BaseActivity {

    PatrolObjectSelectionStates states;
    private ActivityPatrolObjectSelectionBinding mBinding;

    private List<String> patrolObjectList;
    private int selectedPosition = -1; // 当前选中的位置
    private List<TextView> objectButtons;
    private boolean isStartingPatrol = false; // 是否正在执行跳转，防止重复点击
    private String id = "";//巡查工单id

    @Override
    protected void initViewModel() {
        states = getActivityScopeViewModel(PatrolObjectSelectionStates.class);
    }

    @Override
    protected DataBindingConfig getDataBindingConfig() {
        return new DataBindingConfig(R.layout.activity_patrol_object_selection, BR.vm, states)
                .addBindingParam(BR.click, new ClickProxy());
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        mBinding = (ActivityPatrolObjectSelectionBinding) getBinding();
        id = getIntent().getStringExtra("id");
        // 初始化巡查对象列表
        initPatrolObjects();

        // 初始化标题栏
        initTitleBar();

        // 初始化按钮列表
        initButtons();
    }

    /**
     * 初始化标题栏
     */
    private void initTitleBar() {
        mBinding.titlebar.titleBarBack.setOnClickListener(v -> finish());
    }

    /**
     * 初始化巡查对象列表
     */
    private void initPatrolObjects() {
        patrolObjectList = new ArrayList<>();
        patrolObjectList.add("闸墩");
        patrolObjectList.add("边墙");
        patrolObjectList.add("胸墙");
        patrolObjectList.add("工作桥");
        patrolObjectList.add("交通桥");
        patrolObjectList.add("启闭机房");
        patrolObjectList.add("闸门系统");
        patrolObjectList.add("电气与控制系统");
        patrolObjectList.add("上下游连接段");
        patrolObjectList.add("观测设施");
    }

    /**
     * 初始化按钮列表并设置点击事件
     */
    private void initButtons() {
        objectButtons = new ArrayList<>();
        objectButtons.add(mBinding.btnObject1);
        objectButtons.add(mBinding.btnObject2);
        objectButtons.add(mBinding.btnObject3);
        objectButtons.add(mBinding.btnObject4);
        objectButtons.add(mBinding.btnObject5);
        objectButtons.add(mBinding.btnObject6);
        objectButtons.add(mBinding.btnObject7);
        objectButtons.add(mBinding.btnObject8);
        objectButtons.add(mBinding.btnObject9);
        objectButtons.add(mBinding.btnObject10);

        // 设置按钮文本
        for (int i = 0; i < objectButtons.size() && i < patrolObjectList.size(); i++) {
            objectButtons.get(i).setText(patrolObjectList.get(i));
            final int position = i;
            objectButtons.get(i).setOnClickListener(v -> selectObject(position));
        }

        // 设置开始巡查按钮
        mBinding.btnStartPatrol.setOnClickListener(v -> startPatrol());
    }

    /**
     * 选择巡查对象
     */
    private void selectObject(int position) {
        // 取消之前的选中状态
        if (selectedPosition >= 0 && selectedPosition < objectButtons.size()) {
            updateButtonState(objectButtons.get(selectedPosition), false);
        }

        // 设置新的选中状态
        selectedPosition = position;
        updateButtonState(objectButtons.get(position), true);
    }

    /**
     * 更新按钮状态
     */
    private void updateButtonState(TextView button, boolean selected) {
        if (selected) {
            button.setBackgroundResource(R.drawable.ic_manager_work_order_list_oval_bule);
            button.setTextColor(getResources().getColor(R.color.color_219EFF));
        } else {
            button.setBackgroundResource(R.drawable.bg_patrol_object_unselected);
            button.setTextColor(getResources().getColor(R.color.color_666666));
        }
    }

    /**
     * 开始巡查
     */
    private void startPatrol() {
        // 防止重复点击
        if (isStartingPatrol) {
            return;
        }

        if (selectedPosition < 0 || selectedPosition >= patrolObjectList.size()) {
            showToast("请选择巡查对象");
            return;
        }

        // 设置标志位，防止重复点击
        isStartingPatrol = true;

        // 禁用按钮，防止重复点击
        mBinding.btnStartPatrol.setEnabled(false);

        String selectedObject = patrolObjectList.get(selectedPosition);

        // 跳转到巡查页面，传递选中的巡查对象
        Intent intent = new Intent(this, PatrolActivity.class);
        intent.putExtra("patrolObject", selectedObject);
        startActivity(intent);

        // 跳转后finish当前页面
        finish();
    }

    /**
     * 点击事件代理
     */
    public class ClickProxy {
        public void onBackClick() {
            finish();
        }

        public void onStartPatrolClick() {
            startPatrol();
        }
    }

    /**
     * States
     */
    public static class PatrolObjectSelectionStates extends StateHolder {
        public final State<String> titleBar = new State<>("工程巡查");
    }
}

